export default {

	// 单图上传 (个人中心头像和背景)
	uploadImageOdd(type) {
		let imgUrl = new Promise((resolve, reject) => {
			uni.chooseImage({
				count: 1, //默认9
				success: (chooseImageRes) => {
					uni.showLoading({
						title: '上传中...'
					})
					let filePath = chooseImageRes.tempFilePaths[0];
					const result = uniCloud.uploadFile({
						filePath: filePath,
						cloudPath: String(Math.random() * 5).split('.')[1] + '.png',
					})
					result.then(res => {
						uni.hideLoading();
						//获取到上传到云储存的url地址 
						// console.log(res['fileID']);
						let userInfo = uni.getStorageSync("userInfo");
						let data = {
							_id: userInfo._id,
							uid: userInfo.uid,
							avatar: userInfo.avatar,
							name: userInfo.name,
							sex: userInfo.sex,
							age: userInfo.age,
							Introduction: userInfo.Introduction, // 简介
							bgImg: userInfo.bgImg,
							type: 'setting'
						}
						if(type == 'avatar'){
							data.avatar = res['fileID']
						}else if(type == 'bgImg'){
							data.bgImg = res['fileID']
						}
						uniCloud.callFunction({ //客户端调用云函数，云函数调用数据库
							name: 'userTable', //在云函数中的函数名,也就是创建的云函数的文件夹的名字
							data: data,
							success: (res) => {
								// console.log(res);
								uni.hideLoading();
								if (res.result.code == 0) {
									// this.getUserInfo();
									uni.showToast({
										title: res.result.msg,
										icon: 'none'
									})


								}
							},
							error: function(e) {}
						})
						resolve(res['fileID'])
						// that.userInfo.avatar = res['fileID']
					})
				}
			});
		})
		return imgUrl
	},
	// 记录发表图片
	indexImg(num) {
		return new Promise((resolve, reject) => {
			let filth = [];
			uni.chooseImage({
				count: num, //默认9
				success: (chooseImageRes) => {
					// uni.showLoading({
					// 	title: '上传中...'
					// })
					let filePath = chooseImageRes.tempFilePaths;

					resolve(filePath)
				}
			});
		})
	},
	// 预览图片
	viewImg(urls,current) {
		// console.log(urls,current)
		// 预览图片
		uni.previewImage({
			current: current,
			urls: urls,
			longPressActions: {
				itemList: ['发送给朋友', '保存图片', '收藏'],
				success: function(data) {
					console.log('选中了第' + (data.tapIndex + 1) + '个按钮,第' + (data.index + 1) + '张图片');
				},
				fail: function(err) {
					console.log(err.errMsg);
				}
			}
		});
	}

}
